<?php
declare (strict_types = 1);

namespace app\model;

use think\Db;
use think\facade\Config;
use think\Model;

/**
 * @mixin \think\Model
 */
class CurrentStock extends Model
{
    public $table='CurrentStock';
    public $connection='U8';
    public function index($T_WMS_STOCK_INFO='',$T_WMS_SPOT_ITEM_INFO=''){
        $list=$this->alias('cs')
            ->field('
                        max(cs.iQuantity) as iQuantity,
                        max(inv.cInvCode) as cInvCode ,
                        max(inv.cInvStd) as cInvStd
                    ')
            ->where('inv.dEDate is null')
            ->join('inventory inv', 'inv.cInvCode= cs.cInvCode','left')//物料
            ->join('Warehouse w', 'w.cWhCode = cs.cWhCode', 'left')//
            ->group('inv.cInvCode')
            ->paginate(10000, false, [
                'page' => 1
            ]);
//            ->each(function ($v, $k) use ($T_WMS_STOCK_INFO,$T_WMS_SPOT_ITEM_INFO){
//                $v['iQuantity']=intval($v['iQuantity']);
//                //搜索不加仓库
//
//                $list_mes_1 =[];
//                if(!empty($request['cWhCode'])){
//                    $list_mes_1 =[];
//                    $list_mes_2 =$T_WMS_STOCK_INFO->where('ITEM_CODE',$v['cInvCode'])->field('ITEM_CODE,ITEM_SN,ITEM_QTY,STORAGE_CODE')->select()->toArray();
//                }else{
//                    $list_mes_1 = $T_WMS_SPOT_ITEM_INFO->where('ITEM_CODE',$v['cInvCode'])->field("ITEM_CODE,ITEM_SN,ITEM_QTY, '线边仓' as STORAGE_CODE")->select()->toArray();
//                    $list_mes_2 = $T_WMS_STOCK_INFO->where('ITEM_CODE',$v['cInvCode'])->field('ITEM_CODE,ITEM_SN,ITEM_QTY,STORAGE_CODE')->select()->toArray();
//                }
//
//                $list_mes = array_merge($list_mes_1,$list_mes_2);
//
//                $mesQTY =0;
//                foreach ($list_mes as $key => $value) {
//                    $mesQTY+=$value['ITEM_QTY'];
//                }
//                $v['mesQTY'] =$mesQTY;
//
//                $v['cy_number'] = (int)bcsub((string)$v['iQuantity'],(string)$v['mesQTY'],0);
//                return $v;
//            });
        $listToarr = $list->ToArray();

        $cInvCodes=array_column($listToarr['data'],'cInvCode');
        $cInvCodess=[];
        $len=ceil(count($cInvCodes)/2000);
        $i=1;
        foreach ($cInvCodes as $k=>$v){
            if($k<2000*$i){
                $cInvCodess[$i][]=$v;
            }else{
                ++$i;
                $cInvCodess[$i][]=$v;
            }
        }

        $lists1=[];
        $lists2=[];
        $j=1;
        foreach ($cInvCodess as $v){
            //T_WMS_SPOT_ITEM_INFO
            $lists1[$j]=$T_WMS_SPOT_ITEM_INFO->whereIn('ITEM_CODE',$cInvCodess[$j])->field("ITEM_CODE,ITEM_SN,ITEM_QTY, '线边仓' as STORAGE_CODE")->select()->toarray();
            //T_WMS_STOCK_INFO
            $lists2[$j]=$T_WMS_STOCK_INFO->whereIn('ITEM_CODE',$cInvCodess[$j])->field('ITEM_CODE,ITEM_SN,ITEM_QTY,STORAGE_CODE')->select()->toarray();
            //U8
            $lists3[$j]=$this->alias('cs')->join('Warehouse w', 'w.cWhCode= cs.cWhCode','left')->whereIn('cs.cInvCode',$cInvCodess[$j])->field('cs.cInvCode,cs.cWhCode,cs.iQuantity,w.cWhName')->select()->toarray();
            if($len>$j)++$j;
        }
        $lists1=array_merge(...$lists1);
        $lists2=array_merge(...$lists2);
        $lists3=array_merge(...$lists3);
        foreach ($listToarr['data'] as $k=>$v){
            $listToarr['data'][$k]['iQuantity']=intval($v['iQuantity']);
            $list_mes_1 = $this->cInvCodess($lists1,$v['cInvCode']);
            $list_mes_2 = $this->cInvCodess($lists2,$v['cInvCode']);
            $list_mes = array_merge($list_mes_1,$list_mes_2);
            $mesQTY =0;
            $messn ='';

            foreach ($list_mes as $key => $value) {
                $mesQTY+=$value['ITEM_QTY'];
                $messn= 'SN编码:'.trim((string)$value['ITEM_SN']).' SN数量:' . intval($value['ITEM_QTY']).' 库位编码:' . trim((string)$value['STORAGE_CODE']).'　　'.$messn;
            }
            $listToarr['data'][$k]['mesQTY'] =$mesQTY;
            $listToarr['data'][$k]['messn'] =str_replace('　', '<br/>', $messn);

            //下面是u8 ,上面是mes
            $list_u8= $this->cInvCodess2($lists3,$v['cInvCode']);


            $iQuantity = 0;
            $u8code='';
            foreach ($list_u8 as $key => $value) {
                $iQuantity+=$value['iQuantity'];
                $u8code = '仓库:'.trim((string)$value['cWhName']).' 数量:' . intval($value['iQuantity']).'　'.$u8code;
            }
            $listToarr['data'][$k]['iQuantity'] =intval($iQuantity);
            $listToarr['data'][$k]['u8code'] =str_replace('　', '<br/>', $u8code);
            // halt($v);

            $listToarr['data'][$k]['cy_number'] = (int)bcsub((string)$listToarr['data'][$k]['iQuantity'],(string)$listToarr['data'][$k]['mesQTY'],0);
            $listToarr['data'][$k]['cy_number2'] =abs($listToarr['data'][$k]['cy_number']);
        }

        foreach ($listToarr['data'] as $key => $value) {
            if($value['cy_number2'] == 0){
                unset($listToarr['data'][$key]);
            }
        }
        $col = array_column($listToarr['data'],'cy_number2');
        array_multisort($col,SORT_DESC,$listToarr['data']);
        return $listToarr['data'];
    }
    //666666
    public function cInvCodess($list,$ITEM_CODE=''){
        $data=[];
        foreach ($list as $v){
            if($ITEM_CODE==$v['ITEM_CODE']){
                $data[]=$v;
            }
        }
        return $data;
    }
    public function cInvCodess2($list,$ITEM_CODE=''){
        $data=[];
        foreach ($list as $v){
            if($ITEM_CODE==$v['cInvCode']){
                $data[]=$v;
            }
        }
        return $data;
    }
}
